﻿
var common = {};
common.utils = {
    /*把json datetime转为指定格式*/
    parseJsonDateTime : function (dt) {
        return Date.parse(eval(dt.replace(/\//g, ''))).toString('yyyy-MM-dd HH:mm:ss');
    },
    //复制对象
    cloneObj: function (myObj) {
        if (typeof (myObj) != 'object') return myObj;
        if (myObj == null) return myObj;
        var myNewObj;
        if ($.type(myObj) == 'array') {
            myNewObj = [];
        } else {
            myNewObj = new Object();
        }
        for (var i in myObj) {
            myNewObj[i] = this.cloneObj(myObj[i]);
        }
        return myNewObj;
    },
	sleep: function (milliSeconds) {
		var startTime = new Date().getTime();
		while (new Date().getTime() < startTime + milliSeconds);
	},
    getQueryString: function(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
};

var App = function () {
    //点击下拉菜单部分不会隐藏
    var handleDropdowns = function () {
        $('body').on('click', '.dropdown-menu.hold-on-click', function (e) {
            e.stopPropagation();
        });
    }


    // handles portlet action bar functionality(refresh, configure, toggle, remove)
    var handlePanelTools = function () {
        jQuery('body').on('click', '.panel-tool .tools a.fa-times', function (e) {
            e.preventDefault();
            var removable = jQuery(this).parents(".panel-tool");
            if (removable.next().hasClass('panel-tool') || removable.prev().hasClass('panel-tool')) {
                jQuery(this).parents(".panel-tool").remove();
            } else {
                jQuery(this).parents(".panel-tool").parent().remove();
            }
        });

        jQuery('body').on('click', '.panel-tool .tools a.fa-chevron-down, .panel-tool .tools a.fa-chevron-up', function (e) {
            e.preventDefault();
            var el = jQuery(this).closest(".panel-tool").children(".panel-body");
            if (jQuery(this).hasClass("fa-chevron-down")) {
                jQuery(this).removeClass("fa-chevron-down").addClass("fa-chevron-up");
                el.slideUp(200);
            } else {
                jQuery(this).removeClass("fa-chevron-up").addClass("fa-chevron-down");
                el.slideDown(200);
            }
        });
    }

    return {
        init: function () {
            handleDropdowns();
            handlePanelTools();
        },

        // wrapper function to scroll(focus) to an element
        scrollTo: function (el, offeset) {
            var pos = (el && el.size() > 0) ? el.offset().top : 0;

            if (el) {
                if ($('body').hasClass('page-header-fixed')) {
                    pos = pos - $('.header').height(); 
                }            
                pos = pos + (offeset ? offeset : -1 * el.height());
            }

            jQuery('html,body').animate({
                scrollTop: pos
            }, 'slow');
        },

        // function to scroll to the top
        scrollTop: function () {
            App.scrollTo();
        },

        // wrapper function to  block element(indicate loading)
        blockUI: function (options) {
            var options = $.extend(true, {}, options);
            var html = '';
            if (options.iconOnly) {
                html = '<div class="loading-message ' + (options.boxed ? 'loading-message-boxed' : '')+'"><img style="" src="/images/loading-spinner-grey.gif" align=""></div>';
            } else if (options.textOnly) {
                html = '<div class="loading-message ' + (options.boxed ? 'loading-message-boxed' : '')+'"><span>&nbsp;&nbsp;' + (options.message ? options.message : 'LOADING...') + '</span></div>';
            } else {    
                html = '<div class="loading-message ' + (options.boxed ? 'loading-message-boxed' : '')+'"><img style="" src="/images/loading-spinner-grey.gif" align=""><span>&nbsp;&nbsp;' + (options.message ? options.message : 'LOADING...') + '</span></div>';
            }

            if (options.target) { // element blocking
                var el = jQuery(options.target);
                if (el.height() <= ($(window).height())) {
                    options.cenrerY = true;
                }            
                el.block({
                    message: html,
                    baseZ: options.zIndex ? options.zIndex : 1000,
                    centerY: options.cenrerY != undefined ? options.cenrerY : false,
                    css: {
                        top: '10%',
                        border: '0',
                        padding: '0',
                        backgroundColor: 'none'
                    },
                    overlayCSS: {
                        backgroundColor: options.overlayColor ? options.overlayColor : '#000',
                        opacity: options.boxed ? 0.05 : 0.1, 
                        cursor: 'wait'
                    }
                });
            } else { // page blocking
                $.blockUI({
                    message: html,
                    baseZ: options.zIndex ? options.zIndex : 1000,
                    css: {
                        border: '0',
                        padding: '0',
                        backgroundColor: 'none'
                    },
                    overlayCSS: {
                        backgroundColor: options.overlayColor ? options.overlayColor : '#000',
                        opacity: options.boxed ? 0.05 : 0.1,
                        cursor: 'wait'
                    }
                });
            }            
        },

        // wrapper function to  un-block element(finish loading)
        unblockUI: function (target) {
            if (target) {
                jQuery(target).unblock({
                    onUnblock: function () {
                        jQuery(target).css('position', '');
                        jQuery(target).css('zoom', '');
                    }
                });
            } else {
                $.unblockUI();
            }
        },

        startPageLoading: function(message) {
            $('.page-loading').remove();
            $('body').append('<div class="page-loading"><img src="assets/img/loading-spinner-grey.gif"/>&nbsp;&nbsp;<span>' + (message ? message : 'Loading...') + '</span></div>');
        },

        stopPageLoading: function() {
            $('.page-loading').remove();
        },        
    }
}();

var validate = function(){
    return {
        init: function () {
            if(jQuery.validator){
                $.validator.setDefaults({
                    //光标移出时
                    onfocusout: function (element) {
                        this.element(element);
                    },

                    //光标移入时
                    onfocusin: function (element, event) {
                        //仅针对jquery.validate
                        var errorElement = this.errorsFor(element);
                        if (errorElement) {
                            errorElement.remove();
                        }
                    },
                    onkeyup: function (element, event) {
                    },
                    ignore: ".ignore",
                    highlight: function (element) {
                        $(element).closest(".form-group").addClass("has-error");
                    },
                    unhighlight: function (element) {
                        $(element).closest(".form-group").removeClass("has-error");
                    },
                    errorPlacement: function(error, element) {  
                        if (element.parent(".input-group").length>0){
                            //error.appendTo(element.parent().parent().next());
                            if(!element.parent().parent().next().hasClass("form-group-error")){
                                element.parent().parent().after("<div class='form-group-error'><div class='col-sm-2'></div><label></label></div>");         
                            }
                            error.appendTo(element.parent().parent().next());
                        }
                        else{
                            error.appendTo(element.parent());  
                        }
                    }
                });
            }            
        }
    };
}();


; (function ($, window, undefined) {
    var _timer;
    var warp;
    var tip = function (content, type) {
        //alert-success alert-info alert-warning alert-danger   
        var wrap = document.createElement('div'),
            body = document.body;        
        wrap.className = " div-top-tip container alert navbar-fixed-top  alert-" + (typeof type == "undefined" ? "success" : type);
        wrap.style.cssText = 'position:fixed;left:0;top:0;';
        var closeHtml = '<div onclick="tip.close(this)" style="margin-right:4px; top:14px; right:14px; position:absolute; font-size:10px; cursor:pointer;">X</div>';
        wrap.innerHTML = content + closeHtml;        
        body.insertBefore(wrap, body.firstChild);
        $(wrap).css("zIndex", 1990 + document.getElementsByClassName("div-top-tip").length);
        
        _timer = setTimeout(function () {
            $(wrap).remove();
        }, 1000 * 5);
    };
    tip.close = function (elem) {
        var tipObj = elem.parentNode;
        tipObj.parentNode.removeChild(tipObj);
        _timer && clearTimeout(_timer);
    }

    tip.close = function (elem) {
        var tipObj = elem.parentNode;
        tipObj.parentNode.removeChild(tipObj);
        _timer && clearTimeout(_timer);
    }
    tip.danger = function (content) {
        tip(content, "danger");
    }
    tip.warning = function (content) {
        tip(content, "warning");
    }
    tip.info = function (content) {
        tip(content, "info");
    }
    tip.success = function (content) {
        tip(content, "success");
    }
    window.tip = $.tip = $.tip = tip;
})(this.art || this.jQuery && (this.art = jQuery), this);